<?php
// 本类由系统自动生成，仅供测试用途
class IndexAction extends Action {
	public function login(){
		//加载登陆页
		$this->display();
	}
    public function index(){
    		if(empty($_SESSION['username'])){
    			header("Location:login");
    		}
		//加载主页面
		$this->display();
    }
    public function edit(){
    		if(empty($_SESSION['username'])){
    			header("Location:login");
    		}
    		//规定 cate ：1--- 代表推荐专题   2--- 代表热门专题
    		$bigtype = $_GET['bigtype'];
    		$middletype = $_GET['middletype'];
    		$smalltype = $_GET['smalltype'];
    		session("bigtype",$bigtype);
    		session("middletype",$middletype);
    		session("smalltype",$smalltype);
    		
//  		//找到数据渲染到页面
//  		$news = M(news);
//  		$list=$news->where("cate=".$cate)->select();
//  		print_r($list);
		//加载编辑页
		$this->display();
    }
    //提供ajax来获取数据
    function getgoodsData(){
    		$arr['bigtype'] = $_SESSION['bigtype'];
    		$arr['middletype'] = $_SESSION['middletype'];
    		$arr['smalltype'] = $_SESSION['smalltype'];
    		$goods = M("good");
    		$list=$goods->where($arr)->select();
    		
    		$res['data']=$list;
    		$res['bigtype'] = $_SESSION['bigtype'];
    		$res['middletype'] = $_SESSION['middletype'];
    		$res['smalltype'] = $_SESSION['smalltype'];
    		$this->ajaxReturn($res);
    		
    }
    
    
    
    
    //验证用户名是否正确
    public function checkUsers(){
    		$username=$_POST['username'];
    		$pwd=$_POST['pwd'];
    		//连接数据库查询数据
    		$users = M("users");
    		$data['username'] = $username;
    		$data['pwd'] = $pwd;
    		$res = $users->where($data)->find();
    		if($res){
    			$info['info']="用户名密码正确";
    			$info['status']=1;
    			//存session
    			session("username",$username);
    		}else{
    			$info['info']="用户名或密码错误";
    			$info['status']=2;
    		}
    		//返还状态
    		$this->ajaxReturn($info);
    }
    
    //新闻表 添加数据
    function add(){
    		if(!empty($_POST['name'])){
    			
    			$data['name']=$_POST['name'];
    			$data['number']=$_POST['goodnumber'];
    			$data['price']=$_POST['price'];
    			$data['memberprice']=$_POST['memberprice'];
    			$data['sellnum']=$_POST['sellnum'];
    			$data['reviewnum']=$_POST['reviewnum'];
    			$data['score']=$_POST['score'];
    			$data['content']=$_POST['content'];
    			$data['imgurl']=$_POST['imgurl'];
    			$data['type']=$_POST['type'];
    			$data['detailimg']=$_POST['detailimg'];
    			$data['bigtype'] = $_SESSION['bigtype'];
	    		$data['middletype'] = $_SESSION['middletype'];
	    		$data['smalltype'] = $_SESSION['smalltype'];
    			
    			
	    		$goods = M("good");
	    		$res= $goods->add($data);
	    		if($res){
	    			$arr['bigtype'] = $_SESSION['bigtype'];
		    		$arr['middletype'] = $_SESSION['middletype'];
		    		$arr['smalltype'] = $_SESSION['smalltype'];
    				$info['data']=$goods->where($arr)->select();
		    		$info['status']=1;
	    		}else{
	    			$info["info"] = "系统错误";
	    			$info['status']=2;
	    		}
    		}else{
    			$info["info"] = "数据不能为空";
    			$info["data"] = $_POST;
    			$info['status']=3;
    		}
    		$this->ajaxReturn($info);
    }
    
    function del(){
    		$id=$_POST['id'];
    		$goods = M("good");
    		$res= $goods->where('id='.$id)->delete();
    		if($res){
    			$arr['bigtype'] = $_SESSION['bigtype'];
	    		$arr['middletype'] = $_SESSION['middletype'];
	    		$arr['smalltype'] = $_SESSION['smalltype'];
			$info['data']=$goods->where($arr)->select();
    			$info['status']=1;
    		}else{
    			$info['status']=2;
    		}
    		$this->ajaxReturn($info);
    }  
}